﻿using System;
using RazakiBLL;
using RazakiBLL.Managers;
using RazakiBLL.Entities;

namespace RazakiSite.UserControls.Forum
{
    public partial class UCMessageEdit : BaseUserControl
    {
        private UowRazaki _uowRazaki = new UowRazaki();

        #region >>>PROPERTY<<<

        public int TypeId { get; set; }

        public int PermissionId { get; set; }

        protected bool IsReply
        {
            get { return TypeId == _uowRazaki.MessageTypeRepository.GetByType(EMessageType.answer.ToString()).Id; }
        }

        /// <summary>
        /// parentid
        /// </summary>
        public int? MessageId { get; set; }

        public bool AllowSubmit
        {
            set
            {
                if (value)
                {
                    BtnSend.Visible = true;
                    UCLoginMessage.Visible = false;
                }
                else
                {
                    BtnSend.Visible = false;
                    UCLoginMessage.Visible = true;
                }
            }

        }

        public string UrlAndMessage
        {
            set
            {
                UCLoginMessage.Url = value.Split('~')[0];
                UCLoginMessage.Message = value.Split('~')[1];
            }
        }

        #endregion

        #region >>>EVENT<<<

        public delegate void DataAddedHandler(object sender, bool added);

        public event DataAddedHandler DataAdded;

        #endregion

        #region >>>METHOD<<<

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnSend_Click(object sender, EventArgs e)
        {
            string message;
            if (!ValidateControls(out message))
            {
                UCInfo.Text = message;
                UCInfo.InfoType = EInfoType.error;
                UCInfo.Hide = false;

                return;
            }

            MessageForum messageForum;
            string subject = TxtBSubject.Text;
            //string text = TxtBText.Text;
            string text = UCEditor.UCContent;

            messageForum = new MessageForum
                               {
                                   Subject = subject,
                                   Text = text,
                                   UserId = UtilitiesUI.GetCurrentUserId(),
                                   TypeId = TypeId,
                                   PermissionId = PermissionId,
                                   CreatedDate = DateTime.Now,
                                   LastUpdatedDate = DateTime.Now
                               };

            if (MessageId.HasValue)
                messageForum.ParentId = MessageId;
            else
                messageForum.ParentId = null; //جدید ایجاد شده است

            _uowRazaki.MessageForumRepository.Add(messageForum);
            if (_uowRazaki.Save() > 0)
            {
                UCInfo.Text = UtilitiesUI.Sentences(ESentences.success);
                UCInfo.InfoType = EInfoType.success;

                ClearControls();

                if (DataAdded != null)
                    DataAdded(this, true);
            }
            else
            {
                UCInfo.Text = UtilitiesUI.Sentences(ESentences.error);
                UCInfo.InfoType = EInfoType.error;
            }

            UCInfo.Hide = false;
        }

        private bool ValidateControls(out string message)
        {
            string subject = TxtBSubject.Text;
            //string text = TxtBText.Text;
            string text = UCEditor.UCContent;

            message = string.Empty;

            //در حالت پاسخ عنوان اعتبارسنجی نشود.
            if (!IsReply && string.IsNullOrWhiteSpace(subject))
            {
                message = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "عنوان");
                return false;
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                message = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "متن");
                return false;
            }

            return true;
        }
        
        private void ClearControls()
        {
            TxtBSubject.Text =
                UCEditor.UCContent = string.Empty;
        }

        #endregion
    }
}